package com.cgbl.service.adapter

import android.view.View
import android.widget.ImageView
import com.cgbl.service.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.robot.base.model.MemberAdapterBean
import com.robot.base.model.UserInfoBean
import com.robot.base.utils.pic.ImageLoaderUtils

class AddTeamMemberAdapter(private val checkEnable: Boolean = false) :
    BaseQuickAdapter<MemberAdapterBean, BaseViewHolder>(R.layout.app_item_add_team_members) {

    val selectPos = arrayListOf<String>()

    var checkChangeListener: (() -> Unit)? = null

    override fun convert(holder: BaseViewHolder, item: MemberAdapterBean) {

        val checkView = holder.getView<ImageView>(R.id.iv_check)
        checkView.visibility = if (checkEnable) View.VISIBLE else View.GONE
        checkView.setOnClickListener {
            checkView.isSelected = !checkView.isSelected
            if (checkView.isSelected) {
                selectPos.add(holder.adapterPosition.toString())
            } else {
                selectPos.remove(holder.adapterPosition.toString())
            }
            checkChangeListener?.invoke()
        }
        checkView.isSelected = selectPos.contains(holder.adapterPosition.toString())


        holder.setText(R.id.tv_name, item.accountName)
        holder.setText(R.id.tv_career, item.career)
        ImageLoaderUtils.displayCircle(context, holder.getView(R.id.iv_avatar), item.accountHead)
    }

    fun setCheckChange(checkChange: () -> Unit) {
        checkChangeListener = checkChange
    }

    fun allCheck() {
        for (i in 0 until data.size) {
            selectPos.add(i.toString())
        }
        notifyDataSetChanged()
    }

    fun cancelAllCheck() {
        selectPos.clear()
        notifyDataSetChanged()
    }

    fun setDefaultCheck(ids: List<String>) {
        for (i in 0 until data.size) {
            if (ids.contains(data[i].id)) {
                selectPos.add(i.toString())
            }
        }
    }

    fun getSelectedMember(): List<MemberAdapterBean> {
        val selectMembers = mutableListOf<MemberAdapterBean>()
        for (i in 0 until data.size) {
            if (selectPos.contains(i.toString())) {
                selectMembers.add(data[i])
            }
        }
        return selectMembers
    }
}